/**
 * Software License, Version 1.0
 * 
 * Copyright 2003 The Trustees of Indiana University.  All rights reserved.
 * 
 *
 *Redistribution and use in source and binary forms, with or without 
 *modification, are permitted provided that the following conditions are met:
 *
 *1) All redistributions of source code must retain the above copyright notice,
 * the list of authors in the original source code, this list of conditions and
 * the disclaimer listed in this license;
 *2) All redistributions in binary form must reproduce the above copyright 
 * notice, this list of conditions and the disclaimer listed in this license in
 * the documentation and/or other materials provided with the distribution;
 *3) Any documentation included with all redistributions must include the 
 * following acknowledgement:
 *
 *"This product includes software developed by the Community Grids Lab. For 
 * further information contact the Community Grids Lab at 
 * http://communitygrids.iu.edu/."
 *
 * Alternatively, this acknowledgement may appear in the software itself, and 
 * wherever such third-party acknowledgments normally appear.
 * 
 *4) The name Indiana University or Community Grids Lab or NaradaBrokering, 
 * shall not be used to endorse or promote products derived from this software 
 * without prior written permission from Indiana University.  For written 
 * permission, please contact the Advanced Research and Technology Institute 
 * ("ARTI") at 351 West 10th Street, Indianapolis, Indiana 46202.
 *5) Products derived from this software may not be called NaradaBrokering, 
 * nor may Indiana University or Community Grids Lab or NaradaBrokering appear
 * in their name, without prior written permission of ARTI.
 * 
 *
 * Indiana University provides no reassurances that the source code provided 
 * does not infringe the patent or any other intellectual property rights of 
 * any other entity.  Indiana University disclaims any liability to any 
 * recipient for claims brought by any other entity based on infringement of 
 * intellectual property rights or otherwise.  
 *
 *LICENSEE UNDERSTANDS THAT SOFTWARE IS PROVIDED "AS IS" FOR WHICH NO 
 *WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. INDIANA UNIVERSITY GIVES
 *NO WARRANTIES AND MAKES NO REPRESENTATION THAT SOFTWARE IS FREE OF 
 *INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR OTHER PROPRIETARY RIGHTS. 
 *INDIANA UNIVERSITY MAKES NO WARRANTIES THAT SOFTWARE IS FREE FROM "BUGS", 
 *"VIRUSES", "TROJAN HORSES", "TRAP DOORS", "WORMS", OR OTHER HARMFUL CODE.  
 *LICENSEE ASSUMES THE ENTIRE RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR 
 *ASSOCIATED MATERIALS, AND TO THE PERFORMANCE AND VALIDITY OF INFORMATION 
 *GENERATED USING SOFTWARE.
 */
package cgl.narada.samples.rtp;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.media.ControllerErrorEvent;
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.Player;
import javax.media.RealizeCompleteEvent;
import javax.media.protocol.DataSource;

/**
 * PlayerWindow.java
 *
 * This class is used to play a DataSource either audio or video
 *
 * @author Ahmet Uyar
 * $Date$
 */

public class PlayerWindow extends Frame implements ControllerListener{

   public Player player;
   boolean done = false;

   public PlayerWindow(DataSource ds) {

      try{
         player = javax.media.Manager.createPlayer(ds);
         player.addControllerListener(this);
         player.realize();
      }catch(Exception e){
         System.out.println("Can not create the player.");
         e.printStackTrace();
      }

      addWindowListener(new WindowAdapter(){
                              public void windowClosing(WindowEvent we){
                                 close();
                              }
                            }
                           );
   }

   public void initialize() {
       add(new PlayerPanel(player));
   }

   public void close() {
      player.close();
      setVisible(false);
      dispose();
      done = true;
      System.out.println("PlayerWindow is closed.");
   }

   public boolean isDone(){
      return done;
   }

   public void addNotify() {
      super.addNotify();
      pack();
   }

    /**
     * ControllerListener for the Players.
     */
    public synchronized void controllerUpdate(ControllerEvent ce) {

      Player p = (Player)ce.getSourceController();
      if ((p == null) || (player != p) )
         return;

      // Get this when the internal players are realized.
      if (ce instanceof RealizeCompleteEvent) {
         System.out.println("Realize Completed");
         initialize();
         setVisible(true);
         player.start();
         System.out.println("player started");
      }else	if (ce instanceof ControllerErrorEvent) {
         p.removeControllerListener(this);
         close();
         System.err.println("PlayerWindow internal error: " + ce);
      }
    }
}//end of PlayerWindow


/**
* GUI classes for the Player.
*/
class PlayerPanel extends Panel {

   Component vc, cc;

    PlayerPanel(Player p) {

      setLayout(new BorderLayout());
      if ((vc = p.getVisualComponent()) != null)
         add("Center", vc);
      if ((cc = p.getControlPanelComponent()) != null)
         add("South", cc);
   }

   public Dimension getPreferredSize() {

      int w = 0, h = 0;
      if (vc != null) {
         Dimension size = vc.getPreferredSize();
         w = size.width;
         h = size.height;
      }
      if (cc != null) {
         Dimension size = cc.getPreferredSize();
         if (w == 0)
         w = size.width;
         h += size.height;
      }
      if (w < 160)
         w = 160;
      return new Dimension(w, h);
   }

}//end of PlayerPanel

